package programManager;
import codesound.lexer.*;
import codesound.parser.Parser;
import codesound.node.*;
import java.io.*;
import javax.sound.midi.MidiSystem;

public class Compiler
{
	public static void compile(String args)
	{
		//Helper.init();
		try
		{	
			File f = new File("temp.temp");
			FileWriter fw = new FileWriter(f);
			fw.write(args);
			fw.close();
			// Create a Parser instance.
			Parser p =
			new Parser(
                            new Lexer(
                                new PushbackReader(
                                    new FileReader(new File("temp.temp")), 1024)));
			
			
			// Parse the input.
			Start tree = p.parse();
			//System.out.println(tree);
			
			f.delete();
			// Apply the translation.
			//tree.apply(new DepthFirstAdapter());
			tree.apply(new Translation());
			try{
                            
                            if(Translation.move){
                                File midi = new File(Helper.fileName+".midi");
                                FileOutputStream fos = new FileOutputStream(midi);
                                MidiSystem.write(Helper.sq, 1, fos);
                                fos.close();
                                    //Translation.move = true;
                                }
				}catch(IOException e){
					e.printStackTrace();
			}
			//System.out.println(Helper.noteList);
//Savin		
		}
		catch(Exception e)
		{
			//System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}
	
	
}
